package com.example.coffeeseller_2.model.helper;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.alibaba.fastjson.JSONArray;
import com.example.coffeeseller_2.model.beans.Coffee;
import com.example.coffeeseller_2.model.beans.DealRecorder;
import com.example.coffeeseller_2.model.beans.ReportBunker;
import com.example.coffeeseller_2.model.beans.Step;
import com.example.coffeeseller_2.model.contentprovider.MaterialSql;
import com.example.coffeeseller_2.model.databases.DealOrderInfoManager;
import com.example.coffeeseller_2.utils.MyLog;

import java.util.ArrayList;
import java.util.List;

import cof.ac.inter.ContainerConfig;

/**
 * Created by WH on 2018/8/12.
 */

public class TradeOverDealManager {

    private static String TAG = "TradeOverDealManager";
    private Context context;

    public TradeOverDealManager(Context context) {
        this.context = context;
    }

    //更新数据库原料表
    public static String updateMaterial(DealRecorder dealRecorder, Context context) {


        List<ReportBunker> bunkers = new ArrayList<>();
        Coffee coffee = dealRecorder.getCoffee();
        if (coffee == null || coffee.getStepList() == null || coffee.getStepList().size() <= 0) {
            return "";
        }
        if (dealRecorder == null || dealRecorder.getContainerConfigs() == null || dealRecorder.getContainerConfigs().size() <= 0) {
            return "";
        }
        MaterialSql materialSql = new MaterialSql(context);

        int waterUseTotal = 0;
        String waterMaterialId = materialSql.getMaterialIDByContainerID("7");
        for (int i = 0; i < coffee.getStepList().size(); i++) {
            Step step = coffee.getStepList().get(i);

            if (step != null)//&& step.getContainerConfig().getWater_capacity() == 0

                MyLog.d(TAG, "step getName = " + step.getMaterial().getName());
            if (step.getContainerConfig() == null) { //如果没有步骤i

                continue;
            }


            MyLog.d(TAG, "step type = " + step.getMaterial().getName());

            if (step.getMaterial() != null) {


                if (step.getContainerConfig().getWater_capacity() != 0 || TextUtils.equals(waterMaterialId, step.getMaterial().getMaterialID() + "")) {

                    //更新用水量
                    waterUseTotal += step.getContainerConfig().getWater_capacity();

                }


                MyLog.d(TAG, "materialID is " + step.getMaterial().getMaterialID());

                MyLog.d(TAG, "getMaterial_time =  " + step.getContainerConfig().getMaterial_time());

                String sqlRestMaterial = materialSql.getStorkByMaterialID(step.getMaterial().getMaterialID() + "");

                float sqlRestMaterialInt = Float.parseFloat(sqlRestMaterial);

                if (step.getContainerConfig().getMaterial_time() == 0) {


                    continue;
                }


                float mkUseMaterialInt = 0;
                ContainerConfig containerConfig = dealRecorder.getContainerConfigs().get(i);
//                mkUseMaterialInt = (containerConfig.getMaterial_time()) /(float)  step.getContainerConfig().getMaterial_time() * step.getAmount();
                if (dealRecorder.getRadios() != null && dealRecorder.getRadios().containsKey(i)) {
                    mkUseMaterialInt = DealRecorder.getInstance().getRadios().get(i) * step.getAmount();
                } else {
                    mkUseMaterialInt = step.getAmount();
                }

                MyLog.d(TAG, "mkUseMaterialInt =  " + mkUseMaterialInt);
                if (mkUseMaterialInt == 0) {


                    continue;
                }

                Log.e(TAG, " materialID is  " + step.getMaterial().getMaterialID() + " stock is " + sqlRestMaterial + ",used= " + mkUseMaterialInt);

                Long stock = Long.parseLong((int) (sqlRestMaterialInt - mkUseMaterialInt) + "");

                stock = (stock <= 0 ? 0 : stock);

                boolean isUpdateSuccess = materialSql.updateMaterialStockByMaterialId(step.getMaterial().getMaterialID() + "", stock + "");

                MyLog.W(TAG, "update material is " + isUpdateSuccess + ", materialId=" + step.getMaterial().getMaterialID()
                        + ", usedMaterial = " + mkUseMaterialInt + " sqlRestMaterial= " + sqlRestMaterialInt + ", stock=" + (sqlRestMaterialInt - mkUseMaterialInt));

                ReportBunker reportBunker = new ReportBunker();
                int bunkerId = Integer.parseInt(materialSql.getBunkerIDByMaterialD(step.getMaterial().getMaterialID() + ""));

                MyLog.d(TAG, "getMaterialID " + step.getMaterial().getMaterialID() + " , bunkerID =" + bunkerId);
                reportBunker.setBunkerID(bunkerId);
                reportBunker.setUnit(Math.round(mkUseMaterialInt) + "");
                reportBunker.setMaterialStock(Math.round((sqlRestMaterialInt - mkUseMaterialInt)) + "");

                bunkers.add(reportBunker);
            }

        }
/*
* 更新用水量
* */
        if (waterUseTotal != 0 && waterMaterialId != null && !TextUtils.isEmpty(waterMaterialId)) {

            String sqlRestWaterMaterial = materialSql.getStorkByMaterialID(waterMaterialId + "");

            float sqlRestWaterMaterialInt = Float.parseFloat(sqlRestWaterMaterial);


            int waterStock = (int) (sqlRestWaterMaterialInt - waterUseTotal / 10);

            if (waterStock <= 0) {
                waterStock = 0;
            }

            boolean isUpdateSuccess = materialSql.updateMaterialStockByMaterialId(waterMaterialId + "", waterStock + "");

            MyLog.d(TAG, "用水量更新：materialID is" + waterMaterialId + " , rest= "
                    + sqlRestWaterMaterialInt + ", used= " + waterUseTotal / 10
                    + " , stock= " + waterStock + ", isUpdateSuccess= " + isUpdateSuccess);


            int bunkerId = Integer.parseInt(materialSql.getBunkerIDByMaterialD(waterMaterialId + ""));
            ReportBunker reportBunker = new ReportBunker();
            reportBunker.setBunkerID(bunkerId);
            reportBunker.setUnit(Math.round(waterUseTotal) + "");
            reportBunker.setMaterialStock(Math.round(waterStock) + "");

            bunkers.add(reportBunker);
        }

        //更新杯子
        String cupBunkerId = materialSql.getBunkerIDByContainerID("8");
        String cupNum = materialSql.getStorkByBunkersID(cupBunkerId);
        if (!TextUtils.isEmpty(cupNum)) {
            int cupNumInt = Integer.parseInt(cupNum);
            if (cupNumInt > 0) {
                materialSql.updateContact(cupBunkerId, "", "", "", "", "", (cupNumInt - 1) + "", "", "", "", "");

                ReportBunker reportBunkerCup = new ReportBunker();
                reportBunkerCup.setBunkerID(Integer.parseInt(cupBunkerId));
                reportBunkerCup.setUnit(1 + "");
                reportBunkerCup.setMaterialStock((cupNumInt - 1) + "");
                bunkers.add(reportBunkerCup);

            }
        }


        /*
        * 更新数据库本地交易数据库bunker
        * */
        JSONArray jsonArray = (JSONArray) com.alibaba.fastjson.JSONObject.toJSON(bunkers);
        String array = jsonArray.toString();
        dealRecorder.setBunkers(array);

        DealOrderInfoManager.getInstance(context).update(dealRecorder);

        MyLog.W(TAG, "updateMaterial is over");
        return array;
    }
}
